<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends Controller {
	/**
	 * 构造函数
	 */
	public function __construct(){
		parent::__construct();
		$this->model = D("Config");
	}
	/**
	 * 列表
	 * @return [type] [description]
	 */
    public function index(){
        $tab = array(
        	'web_info'	=> '基本信息',
        	'links'		=> '友情链接',
    	);

   
        $this->assign('tabs',$tab);
        $this->assign('active','web_info');
        $this->display();
    }

    /**
     * 获取配置信息
     * @return [type] [description]
     */
    public function web_info(){
    	$type = 'web_info';  
        $info = WebConfig($type); 

        $this->assign('info',$info);  
    	$this->display();
    } 
    
    /**
     * [ajaxConfig description]
     * @return [type] [description]
     */
    public function ajaxConfig(){
    	$params = I("type",'web_info'); 
        
        $url = SELF_WEBURL.__CONTROLLER__.'/'.$params;
        
    	$content = http_response($url); 
    	$this->ajaxReturn(['status'=>1,"content"=>$content]);
    }
    /**
     * 保存数据
     * @return [type] [description]
     */
    public function save_config(){ 
    	$type = I('type','');
        if(IS_POST){ 
            switch ($type) {
                case 'web_info':  
                $data = I('post.data');
                
                    if(isset($_FILES['logo']) && $_FILES['logo']['error'] == 0){
                        $upload             = new \Think\Upload();// 实例化上传类
                        $upload->maxSize    = 3145728;
                        $upload->savePath   = './logo/'; 
                        $upload->exts       = array('jpg', 'gif', 'png', 'jpeg');
                        $upload->replace    = true;
                        $upload->autoSub    = false;
                        $upload->saveName   = 'logo';
                        $upload->subName    = '';
                        // 上传文件 
                        $info   =   $upload->upload();
                        if(!$info) {
                            // 上传错误提示错误信息    
                            $this->error($upload->getError());
                        }else{
                            // 上传成功 获取上传文件信息
                            $data['logo'] = $info['logo']['savepath'].$info['logo']['savename'];
                        } 
                    }
                    $insert = array();
                    foreach($data as $k => $v){
                        $insert[] = array('name'=>$k,'value'=>$v,'type'=>'web_info');
                    } 
                    $affect = $this->model->addAll($insert,true);
                   
                    break;
                case 'links':
                    # code...
                    break;
                
                default:
                    # code...
                    break;
            } 
    	}

    }
}